package algorithm.leetcode.I101to200;

import algorithm.leetcode.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class Q114 {

    Queue<TreeNode> queue = new LinkedList<>();

    public void flatten(TreeNode root) {
        midTraverse(root);
        while (!queue.isEmpty()) {
            TreeNode poll = queue.poll();
            poll.left = null;
            poll.right = queue.peek();
        }
    }

    public void midTraverse(TreeNode node) {
        if (node == null) return;
        queue.add(node);
        midTraverse(node.left);
        midTraverse(node.right);

    }

    public static void main(String[] args) {
        Q114 q114 = new Q114();
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);
        node1.left = node2;
        node1.right = node5;
        node2.left = node3;
        node2.right = node4;
        node5.right = node6;
        q114.flatten(node1);
        System.out.println(node1);
    }

}
